using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using IGlobalist.Cms.Common;
using IGlobalist.Cms.Bll;
using IGlobalist.Cms.Model;

namespace IGlobalist.Cms.Admin.Control
{
    public partial class ContentView : AdminBaseUserControl
    {
        public event EventHandler SaveContent;

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public void DoBindContent(object sender, EventArgs e)
        {
            try
            {
                hdfClassID.Value = Convert.ToString(Session[SessionVariable.Clazz.CurrentID]);
                ContentListView cls = (ContentListView)sender;
                long contentID = Convert.ToInt64(cls.SelectedID);
                IContent content = ContentController.GetContent(contentID);
                ucCmsExtPropertiesForm.PropertyList = content.ExtPropertyList;
                if (ucCmsExtPropertiesForm.Controls.Count < 1) pnlExt.Visible = false; else pnlExt.Visible = true;
                tbAuthorNews.Text = content.AuthorNews;
                tinyMCEContent.Text = content.Content;
                tbSource.Text = content.Source;
                tinyMCESummary.Text = content.Summary;
                tbtag.Text = content.Tags;
                tbTitle.Text = content.Title;
                ddlLanguage.SelectedValue = content.Lang;
                if (content.IsHot == true) cbHot.Checked = true; else cbHot.Checked = false;
                ucContentNoteList.BindNode(content.NoteList);
                ViewState["ClassID"] = content.ClassID;
                ViewState["Content"] = content;
                ViewState["PageID"] = content.PageID;
                ViewState["CategoryID"] = content.CategoryID;
               

            }
            catch (Exception ex)
            {
                Logger.Debug("[[Error]]", ex);
            }
        }

        public void DoCopyContent(object sender, EventArgs e)
        {
            ViewState["Content"] = null;
        }

        public void DoNewContent(object sender, EventArgs e)
        {
            hdfClassID.Value = Convert.ToString(Session[SessionVariable.Clazz.CurrentID]);
            ICmsClass clazz = ClassController.GetClass(Convert.ToInt64(Session[SessionVariable.Clazz.CurrentID]));
            ucCmsExtPropertiesForm.PropertyList = clazz.PropertyList;
            if (ucCmsExtPropertiesForm.Controls.Count < 1) pnlExt.Visible = false; else pnlExt.Visible = true;
            tbAuthorNews.Text="";
            tinyMCEContent.Text = "";
            tinyMCESummary.Text = "";
            tbSource.Text="";
            tbtag.Text="";
            tbTitle.Text = "";
            cbHot.Checked = false;
            ViewState["Content"]=null;
            IGlobalist.Cms.Model.ContentNoteList listnode = new IGlobalist.Cms.Model.ContentNoteList();
            ViewState["contentNoteList"] = null;
            ucContentNoteList.BindNode(listnode);
           
        }

        public void DoHidden(object sender, EventArgs e)
        {
            Visible = false;
        }

        public void DoShow(object sender, EventArgs e)
        {
            Visible = true;
        }

        public void DoSaveContent(object sender, EventArgs e)
        {
              try
                {
                    IContent content = new IGlobalist.Cms.Model.Content();
                    if (ViewState["Content"] != null)
                    {
                        content = (IContent)ViewState["Content"];
                        content.EditedBy = IGlobalistMembership.GetUser().ID;
                        content.AuthorNews = tbAuthorNews.Text;
                        ucCmsExtPropertiesForm.PropertyList = content.ExtPropertyList;
                        content.Content = tinyMCEContent.Text;
                        if (cbHot.Checked == true) content.IsHot = true; else content.IsHot = false;
                        content.Name = tbTitle.Text;
                        content.Summary = tinyMCESummary.Text;
                        content.Source = tbSource.Text;
                        content.Tags = tbtag.Text;
                        content.Title = tbTitle.Text;
                        content.Lang = ddlLanguage.SelectedValue;
                        content.NoteList = ucContentNoteList.NoteList;
                        ContentController.UpdateContent(content);
                    }

                    ViewState["contentNoteList"] = null;

                    if (SaveContent != null)
                    {
                        SaveContent(this, new EventArgs());
                    }

                }
                catch (Exception ex)
                {
                    Logger.Debug("[[Error]]", ex);
                    if (SaveContent != null)
                    {
                        SaveContent(this, new EventArgs());
                    }
                }
            
        }

        public void DoSendApproverContent(object sender, EventArgs e)
        {
            if (ViewState["Content"] != null)
            {
                IContent content = (IContent)ViewState["Content"];
                content.Status = ContentStatus.Approving;
                ContentController.UpdateContent(content);

            }
        }

        public void DoSendEditContent(object sender, EventArgs e)
        {
            if (ViewState["Content"] != null)
            {
                IContent content = (IContent)ViewState["Content"];
                content.Status = ContentStatus.Editing;
                ContentController.UpdateContent(content);

            }
        }

        public void DoSendAuthorContent(object sender, EventArgs e)
        {
            if (ViewState["Content"] != null)
            {
                IContent content = (IContent)ViewState["Content"];
                content.Status = ContentStatus.Creating;
                ContentController.UpdateContent(content);

            }
        }

        public void DoPulicContent(object sender, EventArgs e)
        {
            if (ViewState["Content"] != null)
            {
                IContent content = (IContent)ViewState["Content"];
                content.Status = ContentStatus.Publishing;
                ContentController.UpdateContent(content);

            }
        }

        }

    }

